package com.example.monotarr;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Preferencies extends Main {

	private boolean inicial = true;
//	private int r, g, b;
//	private AlertDialog alert2;
//	private View layoutMenu;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		
		
		setContentView(R.layout.preferencies);
		

		this.title2.setText(R.string.preferencies);
		this.but.setVisibility(View.GONE);
		this.but2.setVisibility(View.GONE);
		this.but3.setVisibility(View.GONE);
		
		ListView lv = getListView();
		lv.setVisibility(View.GONE);

		final AlertDialog segur = new AlertDialog.Builder(this).create();
		segur.setTitle("Segur que vols esborrar totes les tasques?");

		segur.setButton("D'acord", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				esborraBD();
				Toast.makeText(Preferencies.this,
						"Totes les tasques han estat esborrades",
						Toast.LENGTH_SHORT).show();
				return;
			}
		});

		segur.setButton2("Cancel·la", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		
		

		Button b1 = (Button) findViewById(R.id.button1);
		b1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				segur.show();

			}
		});
		
		
		
		final AlertDialog segur2 = new AlertDialog.Builder(this).create();
		segur2.setTitle("Segur que vols esborrar totes les tasques fetes?");

		segur2.setButton("D'acord", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				esborra_marcadesBD();
				Toast.makeText(Preferencies.this,
						"Totes les tasques fetes han estat esborrades",
						Toast.LENGTH_SHORT).show();
				return;
			}
		});

		segur2.setButton2("Cancel·la", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		
		
		Button b3 = (Button) findViewById(R.id.button3);
		b3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				segur2.show();
			}
		});
		
		Button b2 = (Button) findViewById(R.id.button2);
		b2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
		        finish();
	    		Intent intent = new Intent();
	    		intent.setClass(Preferencies.this, Main.class);
	    		startActivity(intent);				

			}
		});

		// Spinner
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);

		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.ordre, android.R.layout.simple_spinner_item);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinner.setAdapter(adapter);

		int position;
		if (ordre.equals("id"))
			position = 0;
		else if (ordre.equals("prioritat, id"))
			position = 1;
		else
			position = 2;

		spinner.setSelection(position);

		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int pos, long id) {

				if (!inicial) {
					String s;
					if (pos == 2) {
						s = "any, mes, dia, hora, minut, id";
						Toast.makeText(Preferencies.this,
								"Ordre cronològic per defecte",
								Toast.LENGTH_SHORT).show();
					} 
					else if (pos == 1) {
						s = "prioritat, id";
						Toast.makeText(Preferencies.this,
								"Ordre segons prioritat per defecte",
								Toast.LENGTH_SHORT).show();
					} 
					else {
						s = "id";
						Toast.makeText(Preferencies.this,
								"Ordre d'addició per defecte",
								Toast.LENGTH_SHORT).show();
					}
					canviaOrdreBD(s);

				} 
				else
					inicial = false;

			}

			public void onNothingSelected(AdapterView<?> adapterView) {
				return;
			}
		});

		SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
		final TextView seekBarValue = (TextView) findViewById(R.id.seekbarvalue);

		seekBarValue.setText("Mida actual: " + (int) mida );
		seekBarValue.setTextSize(mida);

		seekBar.setProgress((int) mida - 10);


		seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {

				seekBarValue.setText("Mida actual: " + String.valueOf(progress + 10));
				seekBarValue.setTextSize(progress + 10);
				canviar_mida(progress + 10);
			}

			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				Toast.makeText(Preferencies.this, "Mida canviada",
						Toast.LENGTH_SHORT).show();
			}

		});
		
		
		
		
		
		
		
		
		
		
	
		
		
		
		
		
		
		
		
		

		
		
		/* colors
		
		LayoutInflater inflater =  (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
		final View layout = inflater.inflate(R.layout.dialeg_color, (ViewGroup) findViewById(R.id.rlayoutC));
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Escull un color");
		builder.setView(layout);

		builder.setPositiveButton("D'acord", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				canviarColor();
				return;
			}
		});
		builder.setNegativeButton( "Cancel·la", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});

		final AlertDialog alert = builder.create();
				
				
		Button bc = (Button) findViewById(R.id.buttonColor);
		bc.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(1);

			}
		});
		
		
		

		
		

		
		
		
		
		
		
		
		//PALETA RGB
		
	    SeekBar sb1 = (SeekBar) layout.findViewById(R.id.sb1);	  
	    SeekBar sb2 = (SeekBar) layout.findViewById(R.id.sb2);
	    SeekBar sb3 = (SeekBar) layout.findViewById(R.id.sb3);

	    final TextView t1 = (TextView) layout.findViewById(R.id.valor1);
	    final TextView t2 = (TextView) layout.findViewById(R.id.valor2);
	    final TextView t3 = (TextView) layout.findViewById(R.id.valor3);
	    
	    final TextView t1t = (TextView) layout.findViewById(R.id.r1);
	    final TextView t2t = (TextView) layout.findViewById(R.id.g1);
	    final TextView t3t = (TextView) layout.findViewById(R.id.b1);
	    

	    
	 	sb1.setProgress(50);
		sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				t1.setText(progress+"");
				r = progress;
				layout.setBackgroundColor(Color.rgb(r, g, b));
				
				
				if ((r + g + b)/3 >= 255/2) {

					txToBlack(t1);
					txToBlack(t2);
					txToBlack(t3);
					txToBlack(t1t);
					txToBlack(t2t);
					txToBlack(t3t);
					
				}
				else {

					txToWhite(t1);
					txToWhite(t2);
					txToWhite(t3);
					txToWhite(t1t);
					txToWhite(t2t);
					txToWhite(t3t);
				}
				
				
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}

		});

		
	 	sb2.setProgress(50);
		sb2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				t2.setText(progress+"");
				g = progress;
				layout.setBackgroundColor(Color.rgb(r, g, b));
				
				if ((r + g + b)/3 >= 255/2) {

					txToBlack(t1);
					txToBlack(t2);
					txToBlack(t3);
					txToBlack(t1t);
					txToBlack(t2t);
					txToBlack(t3t);

					
				}
				else {

					txToWhite(t1);
					txToWhite(t2);
					txToWhite(t3);
					txToWhite(t1t);
					txToWhite(t2t);
					txToWhite(t3t);
				}
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}

		});
		
	 	sb3.setProgress(50);
		sb3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				t3.setText(progress+"");
				b = progress;
				layout.setBackgroundColor(Color.rgb(r, g, b));
				
				if ((r + g + b)/3 >= 255/2) {

					txToBlack(t1);
					txToBlack(t2);
					txToBlack(t3);
					txToBlack(t1t);
					txToBlack(t2t);
					txToBlack(t3t);
				}
				else {

					txToWhite(t1);
					txToWhite(t2);
					txToWhite(t3);
					txToWhite(t1t);
					txToWhite(t2t);
					txToWhite(t3t);
				}
			}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			public void onStopTrackingTouch(SeekBar seekBar) {}

		});
		
		
		
		//Drawable d = getResources().getDrawable(R.drawable.gradient);
//		float h =  ((ShapeDrawable)d).getIntrinsicHeight();


		
//	    ShapeDrawable bgShape = ((ShapeDrawable) d).getBackground();
//	    bgShape.getPaint().setColor(Color.BLACK);

		
//		GradientDrawable gd = (GradientDrawable) getResources().getDrawable(R.drawable.gradient);
//	     //  gd.setColor()
//	       
//	       int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };
//
//	       gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
//
//	      
//	      LinearLayout main = new LinearLayout(this);
//       
//      
//	       main.setBackgroundDrawable(gd);
//	       
	       
	*/    
	    

	} // FI ON CREATE
	
	
	/* colors
	@Override
	protected void onPrepareDialog ( int id, Dialog dialog ) {
	    switch ( id ) {
	        case 1:
	            AlertDialog alertDialog = ( AlertDialog ) dialog;
	            alertDialog.setMessage("AAA" );
	            
	            
	            
	            
	    		RadioButton barra1 = (RadioButton) layoutMenu.findViewById(R.id.colorG);
	    		RadioButton barra2 = (RadioButton) layoutMenu.findViewById(R.id.colorS);
	    		RadioButton central1 = (RadioButton) layoutMenu.findViewById(R.id.colorG2);
	    		RadioButton central2 = (RadioButton) layoutMenu.findViewById(R.id.colorS2);
	    		
	    		if (barra1.isChecked()) {
	    			Button b = (Button) layoutMenu.findViewById(R.id.buttonBarraG1);
	    			b.setVisibility(View.VISIBLE);
	    			
	    			TextView t = (TextView) layoutMenu.findViewById(R.id.gr1);
	    			t.setVisibility(View.VISIBLE);
	    			
	    			LinearLayout ll = (LinearLayout)layoutMenu.findViewById(R.id.aaa);
	    			ll.setWeightSum(100);
	    		}
	    		
	    		else if (barra2.isChecked()) {
	    			Button b = (Button) layoutMenu.findViewById(R.id.buttonBarraG1);
	    			b.setVisibility(View.GONE);
	    			
	    			TextView t = (TextView) layoutMenu.findViewById(R.id.gr1);
	    			t.setVisibility(View.GONE);
	    			
	    			LinearLayout ll = (LinearLayout)layoutMenu.findViewById(R.id.aaa);
	    			ll.setWeightSum(50);
	    		}
	    		
	    		
	    		
				LayoutInflater inflater2 =  (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
				layoutMenu = inflater2.inflate(R.layout.dialeg_color_menu, (ViewGroup) findViewById(R.id.rlayoutCM));
				
				AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
				builder2.setTitle("Configura els colors");
				builder2.setView(layoutMenu);
				
				
				builder2.setPositiveButton("D'acord", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						//canviarColor();
						return;
					}
				});
				builder2.setNegativeButton( "Cancel·la", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});
				
				dialog = builder2.create();
	            
	            
	            break;
	    }
	    super.onPrepareDialog( id, dialog );
	}

	@Override
	public Dialog onCreateDialog(int dialogId) {
	    AlertDialog dialog;
	    switch(dialogId) {
	    case 1:
	    	
	    	
	    	
			LayoutInflater inflater2 =  (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
			layoutMenu = inflater2.inflate(R.layout.dialeg_color_menu, (ViewGroup) findViewById(R.id.rlayoutCM));
			
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
			builder2.setTitle("Configura els colors");
			builder2.setView(layoutMenu);
			
			
			builder2.setPositiveButton("D'acord", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					//canviarColor();
					return;
				}
			});
			builder2.setNegativeButton( "Cancel·la", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					return;
				}
			});
			
			dialog = builder2.create();
	    	
	        break;
	    default:
	        dialog = null;
	    }
	    return dialog;
	}

	
	
	
	
	
	
	private void txToBlack(TextView t) {		
		t.setTextColor(Color.rgb(0, 0, 0));
	}
	
	private void txToWhite(TextView t) {		
		t.setTextColor(Color.rgb(255, 255, 255));
	}

	private void canviarColor() {
		
	    shapeD = new ShapeDrawable(new RectShape());	    
	    shapeD.getPaint().setShader(new LinearGradient(0, 0, 0, 771, Color.parseColor("#9F3D3D"), Color.parseColor("#FFFFFF"), Shader.TileMode.REPEAT));
		findViewById(R.id.mainP).setBackgroundDrawable(shapeD);
		colorCanviat = true;
		
	}

	@Override
	public void onWindowFocusChanged(boolean hasWindowsFocus) {
		altura = findViewById(R.id.mainP).getHeight();
		Log.d("ALTURA LinearLayout", altura+"");
	}
	

	public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

		   //  inicial = true;
//		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//		    Toast.makeText(this, "girat", Toast.LENGTH_SHORT).show();
//		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//		    Toast.makeText(this, "dret", Toast.LENGTH_SHORT).show();
//		}
//    }

*/

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {

			finish();
			Intent intent = new Intent();
			intent.setClass(Preferencies.this, Main.class);
			startActivity(intent);

		}
		return super.onKeyDown(keyCode, event);
	}

}